/* eslint-disable */
const barcode = {
  barCodeType: (input) => {
    let prefix = input.substring(0, 1)
    let prefix2 = input.substring(0, 2)
    let prefix4 = input.substring(0, 4)
    let prefix3 = input.substring(0, 3)

    if (prefix === "+") {
      return "HIBC"
    } else if (prefix === "(") {
      return "CODE128"
    } else if (((prefix3 === "012" || prefix2 === "FA") && (input.length == 24 || input.length == 23)) || (prefix2 === "F4" && input.length == 11)) {
      return "RFID"
    }
    //12.19add  通知单扫描（ASN开头）
    else if (input.indexOf("ASN") >= 0 || prefix2 === "AN") {
      return "ASN"
    } else if (prefix2 === "69" && input.length == 13) {
      return "NS"//国标
    } else if (prefix4 === "GTRK") {
      return "GTRK" //跟台入库包码
    } else if (prefix2 === "SN" || (prefix3 === "010" && input.length == 24) || (prefix === "G" && input.length == 11)) {
      return "SN" //
    } else if (prefix2 === "RA" || prefix2 === "CA") {
      return "RA" //
    } else {
      return "GTIN"
    }
  },
  gtinAnalysis: (input) => {
    let rules = [
      //编码规则，通过模板进行校验，运用贪婪模式获取最优解
      //01 10 17
      {
        pattern: /(01\d{13,14})(10.{1,20})(17\d{6})/igm,
        groupCount: 3,
        fields: ["gtin", "lot", "expiredDate"]
      },
      //01 17 10 91
      {
        pattern: /(01\d{13,14})(17\d{6})(10.{1,20})(91.{1,20})/igm,
        groupCount: 4,
        fields: ["gtin", "expiredDate", "lot", "sn"]
      },
      //01 10 17 21
      {
        pattern: /(01\d{13,14})(10.{1,20})(17\d{6})(21.{1,20})/igm,
        groupCount: 4,
        fields: ["gtin", "lot", "expiredDate", "sn"]
      },
      //01 17 11 21
      {
        pattern: /(01\d{13,14})(17\d{6})(11\d{6})(21.{1,20})/igm,
        groupCount: 4,
        fields: ["gtin", "expiredDate", "manufactureDate", "sn"]
      },
      //01 17 10
      {
        pattern: /(01\d{13,14})(17\d{6})(10.{1,20})/igm,
        groupCount: 3,
        fields: ["gtin", "expiredDate", "lot"]
      },
      //01 17  30* 10
      {
        pattern: /(01\d{13,14})(17\d{6})(30\d{1,8})(10.{1,22})/igm,
        groupCount: 5,
        fields: ["gtin", "expiredDate", "qty", "lot", "sn"]
      },
      //01 17 21
      {
        pattern: /(01\d{13,14})(17\d{6})(21.{1,20})/igm,
        groupCount: 3,
        fields: ["gtin", "expiredDate", "sn"]
      },
      //01 11 21
      {
        pattern: /(01\d{13,14})(11\d{6})(21.{1,20})/igm,
        groupCount: 3,
        fields: ["gtin", "manufactureDate", "sn"]
      },
      //01 10 17 11 21
      {
        pattern: /(01\d{13,14})(10.{1,20})(17\d{6})(11\d{6})(21.{1,20})/igm,
        groupCount: 5,
        fields: ["gtin", "lot", "expiredDate", "manufactureDate", "sn"]
      },
      //01 17 10 11 21
      {
        pattern: /(01\d{13,14})(17\d{6})(10.{1,20})(11\d{6})(21.{1,20})/igm,
        groupCount: 5,
        fields: ["gtin", "expiredDate", "lot", "manufactureDate", "sn"]
      },
      //01 10 11 21
      {
        pattern: /(01\d{13,14})(10.{1,20})(11\d{6})(21.{1,20})/igm,
        groupCount: 4,
        fields: ["gtin", "lot", "manufactureDate", "sn"]
      },
      //01 17* 21
      {
        pattern: /(01\d{13,14})(17.{12})(21.{1,20})/igm,
        groupCount: 3,
        fields: ["gtin", "expiredDate", "sn"]
      },
      //01 17* 11 21
      {
        pattern: /(01\d{13,14})(17.{12})(11\d{6})(21.{1,20})/igm,
        groupCount: 4,
        fields: ["gtin", "expiredDate", "manufactureDate", "sn"]
      },
      //01 17* 11* 21
      {
        pattern: /(01\d{13,14})(17.{12})(11.{12})(21.{1,20})/igm,
        groupCount: 4,
        fields: ["gtin", "expiredDate", "manufactureDate", "sn"]
      },

      //01 11* 17* 10    010761332705695211190926172109251019269AE2
      {
        pattern: /(01\d{13,14})(11\d{6})(17\d{6})(10.{1,20})/igm,
        groupCount: 4,
        fields: ["gtin", "manufactureDate", "expiredDate", "lot"]
      },

      //11190926172109251019269AE2
      {
        pattern: /(11\d{6})(17\d{6})(10.{1,20})/igm,
        groupCount: 3,
        fields: ["manufactureDate", "expiredDate", "lot"]
      },
    ]
    let result = {}
    //遍历模板
    for (let i = 0; i < rules.length; i++) {
      let rule = rules[i]
      //通过模板进行正则的匹配，获得最优解
      let matches = rule.pattern.exec(input)
      if (!matches) {
        continue
      }
      //因为得到的matches会包含字符串本身所以要+1
      if (matches.length != rule.groupCount + 1) {
        continue
      }
      //将获取到的结果存入result
      for (let j = 1; j < matches.length; j++) {
        let field = rule.fields[j - 1]
        result[field] = matches[j]
      }
    }
    let results = Object.keys(result).forEach(objKey => {
      let data = result[objKey]
      if (data.startsWith("17") || data.startsWith("12")) {
        let year = data.substring(2, 4)
        let mouth = data.substring(4, 6)
        let day = data.substring(6, 8)
        if (day == "00") {
          if (mouth == "02") {
            if (year % 4 == 0) {
              day = "29"
            } else {
              day = "28"
            }
          } else if (mouth == "04" || mouth == "06" || mouth == "09" || mouth == "11") {
            day = "30"
          } else {
            day = "31"
          }
        }
        result[objKey] = data.substring(0, 2) + "20" + year + "-" + mouth + "-" + day
      }
      if (data.startsWith("11")) {
        let year = data.substring(2, 4)
        let mouth = data.substring(4, 6)
        let day = data.substring(6, 8)
        if (day == "00") {
          day = "01"
        }
        result[objKey] = data.substring(0, 2) + "20" + year + "-" + mouth + "-" + day
      }
    })
    Object.keys(result).forEach(objKey => {
      result[objKey] = result[objKey].substring(2)
    })
    return result
  }
}

export default barcode
